from easygui import *
import sys
import time
import cPAMIE as PAMIE
import cReport

# Initialize our stuff.
rep = cReport.Report()
rep.initialize()
ie = PAMIE.PAMIE()
ie.showDebugging = True
rep.status ("Starting...!")

# globals variables
USR = None
PAS = None
TO = None
MSG = None
sumUSR = str(USR)
sumMSG= str(MSG)
sumTO= str(TO)
currentTime = str(time.localtime())

#-----------------------------------
# Simple intro Message box function.
#

def msgboxOpen(message="Please make sure there is an internet connection available!", title="VODApp", buttonMessage="Continue"):
	rep.status ("*** DEBUG: in msgBox()")
	choices = [buttonMessage]
	reply = buttonbox(message, title, choices)
	return reply

#-----------------------------------
# Simple Info Message box function: a summary of data entered and used by vodafone.
#

def msgboxSummary(message="Summary details", title="VODApp", text=[]):
	global sumUSR, sumMSG, sumTO, currentTime
	sumUSR = str(USR)
	sumMSG= str(MSG)
	sumTO= str(TO)
	currentTime
	
	text=["A summary of the details you sent...\n\n",
		  "\t\nMessage sent at: ", currentTime,
		  "\t\nMessage From: ",sumUSR,
		  "\t\nMessage Text: ", sumMSG,
		  "\t\nSent to: ", sumTO]

	"""
	The text parameter should be a string, or a list or tuple of lines to be
	displayed in the textbox.
	"""
	textbox(message, title, text, codebox=1 )

#-----------------------------------
# The User & Pass GUI function.
#

def UserPassGUI():  #define our vodafone sign in Function
	rep.status("*** DEBUG: in UserPassGUI()")
	try:
#		while 1:
			msg = "VODApp will logon to www.vodafone.ie with your account details and attempt to send an SMS via your web account. Please fill in all fields!"
			title = "VODApp"
			
			fieldNames = ["User Name (#)","Password"]
			fieldValues = []  # we start with blanks for the values
			fieldValues = multpasswordbox(msg,title, fieldNames)

			# make sure that none of the fields was left blank
			while 1:
			    if fieldValues == None: break
			    errmsg = ""
			    for i in range(len(fieldNames)):
				    if fieldValues[i].strip() == "":
					    errmsg = errmsg + ('"%s" is a required field.\n' % fieldNames[i])
			    if errmsg == "": break # no problems found
			    fieldValues = multpasswordbox(errmsg, title, fieldNames, fieldValues)


			global USR, PAS 
			USR = fieldValues[0]
			PAS = fieldValues[1]
			print "\t *** User:", USR
			print "\t *** Pass:", PAS

	except:
		print 'GUI USER&PASS: QUIT!'



#-----------------------------------
# The MSG GUI function.
#

def MsgGUI():  #define our vodafone sign in Function
	rep.status("*** DEBUG: in MsgGUI()")
	try:
#		while 1:
			msg = "Enter SMS data.\n"
			title = "Vodafone Application"

			fieldNames = ["Recipient","SMS Message"]
			fieldValues = []  # we start with blanks for the values
			fieldValues = multenterbox(msg,title, fieldNames)

			# make sure that none of the fields was left blank
			while 1:
			    if fieldValues == None: break
			    errmsg = ""
			    for i in range(len(fieldNames)):
				    if fieldValues[i].strip() == "":
					    errmsg = errmsg + ('"%s" is a required field.\n' % fieldNames[i])
			    if errmsg == "": break # no problems found
			    fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)

			global TO, MSG 
			TO = fieldValues[0]
			MSG = fieldValues[1]

			print "\t *** Recipient:", TO
			print "\t *** Message  :", MSG
	
	except:
		print 'GUI MSG: QUIT!'



#-----------------------------------
# The signIn function Class.
#

def signIn():  #define our vodafone sign in Function
	rep.status("*** DEBUG: in signIn()")
	rep.status ("Signing In with details...")
	try:

		print "\t *** User:", USR
		print "\t *** Pass:", PAS			
		ie.navigate("www.vodafone.ie/services/messaging/webtext/index.jsp")
#		ie.navigate (os.path.abspath(pathName) + "/x.htm")
		rep.status ("URL Opened.")
		rep.status ("Entered User and Pass...")

		rep.verify ("username", ie.textBoxSet("username", USR), True)
		rep.verify ("password", ie.textBoxSet("password", PAS), True)
		
		myElement = ie.elementFind("input", "alt", "Submit")
		ie.elementClick(myElement)
		rep.status ("Login: OK!")

		
		try:
			ie.linkClick ("Web texts:")
			rep.status ("Selected 'Web texts' URL.")

			myElement = ie.elementFind("TEXTAREA", "name", "A46e58445103414217")
			ie.elementClick(myElement)  			# selects the textbox...
			rep.status ("Found and selected SMS AREA.")
				
			# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			# myForm = ie.formName = C2831302	# N.B vodafone forms may have dynamic names, if so get the form name eachtime and store.
			# myForm = formsGetValue (attribute, [filter])
			# myRemaingSMS = ie.formName = None("TEXT", "Messages Remaining")
			# rep.status (myRemaingSMS)
			# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
		
			
			# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ENTER YOUR DATA HERE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			# Sets the text of [ID] box
			ie.textAreaSet("A46e58445103414217", MSG)	# <---- SMS Message placed into box id: 'A46e58445103414217'
			rep.status ("Entered SMS-Message.")

			myElement = ie.elementFind("input", "name", "a0f05090a010004021")
			ie.elementClick(myElement)  			# selects the nuberbox...
			rep.status ("Found and selected Number AREA.")
			ie.textBoxSet("a0f05090a010004021", TO)	# <---- recipient # placed into box id: 'a0f05090a010004021'
			rep.status ("Entered recipient's number.")
			
			myElement = ie.elementFind("input", "alt", "Send Now")
			rep.status ("Found and selected Send button.")

			ie.elementClick(myElement)
			rep.status ("SMS Sent !")
			rep.status ("Message was:")
			rep.status (MSG)

			
		except:
			print 'Error inputing data on SMS page!'
			
	except:
		print 'Login: FAILED'




#-----------------------------------
# The MAIN() function.
#

if __name__ == '__main__':
	msgboxOpen()		# call the opening box
	UserPassGUI()	# call the UserPassGUI
	MsgGUI()		# call the startGUI function
#	signIn() 		# call the signIn function
	msgboxSummary()	# call the msgboxSummary function: a summary of data entered and used by vodafone.

